package projectTurtle;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Date;
import com.toedter.calendar.*;
import java.sql.*;
import java.util.Vector;

public class StockDetailsPanel extends JPanel
{
	JLabel mediaStockIdLbl=null, mediaTypeIdLbl=null, brandLbl=null, 
					barcodeLbl=null, serialNoLbl=null, descriptionLbl=null, catIdLbl=null,
					titleLbl=null, statusLbl=null, releaseDateLbl=null, genreLbl=null,
					runTimeLbl=null, ageRestLbl=null, directorLbl=null, actor1Lbl=null,
					actor2Lbl=null, dateLastRentedLbl=null;
	JTextField  mediaStockIdTxt=null, mediaTypeIdTxt=null,barcodeTxt=null, brandTxt=null,
					serialNoTxt=null, descriptionTxt=null, titleTxt=null, catIdTxt=null,
					runTimeTxt=null, ageRestTxt=null, directorTxt=null,
					actor1Txt=null, actor2Txt=null, releaseDateTxt=null;
	JDateChooser lastJdc;
	JComboBox genreJcb=null, statusJcb=null, stockTypeJcb=null, catIdJcb=null;
	Font myFont=null;
	JPanel baseP=null;
	Connection Conn;
	Vector catIdV=null, stockTypeV=null;
	String queryStockType =	"SELECT stockTypeName FROM stockTypeTable";
	String queryCatId =	"SELECT catName FROM priceCatTable";
	
	public StockDetailsPanel(Connection Conn) 
	{
		this.Conn = Conn;
		baseP = new JPanel();
		
		myFont = new Font("Arial", Font.BOLD, 10);
		mediaStockIdTxt = new JTextField(30);
		mediaStockIdTxt.setFont(myFont);
		mediaStockIdTxt.setEditable(false);
			
		stockTypeV = new Vector();
		populateStockTypeV();
		for(int i = 0; i < stockTypeV.size(); i++)
		{
			System.out.println(""+stockTypeV.elementAt(i));
		}
		stockTypeJcb = new JComboBox(stockTypeV);
		barcodeTxt = new JTextField(30);
		barcodeTxt.setFont(myFont);
		brandTxt = new JTextField(30);
		brandTxt.setFont(myFont);
		serialNoTxt = new JTextField(30);
		serialNoTxt.setFont(myFont);
		descriptionTxt = new JTextField(30);
		descriptionTxt.setFont(myFont);
		titleTxt = new JTextField(30);
		titleTxt.setFont(myFont);
		titleTxt.addKeyListener(
			new KeyAdapter()
			{
				public void keyTyped(KeyEvent e)
				{
					char ch = e.getKeyChar();
					if((ch<'a'||ch>'z')&&(ch<'A'||ch>'Z')&&(ch!=' ')&&(ch!=',')&&(ch!='.')&&(ch!='!')) 
					{
						e.consume();
					}
				}
			}
			);
		catIdV = new Vector();
		populateCatV();
		catIdJcb = new JComboBox(catIdV);
		runTimeTxt = new JTextField(30);
		runTimeTxt.setFont(myFont);
		ageRestTxt = new JTextField(30);
		ageRestTxt.setFont(myFont);
		ageRestTxt.addKeyListener(
			new KeyAdapter()
			{
				public void keyTyped(KeyEvent e)
				{
					char ch = e.getKeyChar();
					if((ch<'a'||ch>'z')&&(ch<'A'||ch >'Z')&&(ch!=' ')&&(ch<'0'||ch>'9'))
					{
						e.consume();
					}
				}
			}
			);
		directorTxt = new JTextField(30);
		directorTxt.setFont(myFont);
		directorTxt.addKeyListener(
			new KeyAdapter()
			{
				public void keyTyped(KeyEvent e)
				{
					char ch = e.getKeyChar();
					if((ch < 'a' || ch > 'z') && (ch < 'A' || ch > 'Z') && (ch != ' '))
					{
						e.consume();
					}
				}
			}
			);
		actor1Txt = new JTextField(30);
		actor1Txt.setFont(myFont);
		actor1Txt.addKeyListener(
			new KeyAdapter()
			{
				public void keyTyped(KeyEvent e)
				{
					char ch = e.getKeyChar();
					if((ch<'a'||ch>'z')&&(ch<'A'||ch>'Z')&&(ch!=' '))
					{
						e.consume();
					}
				}
			}
			);
		actor2Txt = new JTextField(30);
		actor2Txt.setFont(myFont);
		actor2Txt.addKeyListener(
			new KeyAdapter()
			{
				public void keyTyped(KeyEvent e)
				{
					char ch = e.getKeyChar();
					if((ch<'a'||ch>'z')&&(ch<'A'||ch>'Z')&&(ch!=' '))
					{
						e.consume();
					}
				}
			}
			);
		releaseDateTxt = new JTextField(30);
		releaseDateTxt.addKeyListener(
			new KeyAdapter()
			{
				public void keyTyped(KeyEvent e)
				{
					char ch = e.getKeyChar();
					if(ch<'0'||ch>'9')
					{
						e.consume();
					}
				}
			}
			);
		releaseDateTxt.setFont(myFont);
		
		mediaStockIdLbl = new JLabel(" Stock ID ");
		mediaTypeIdLbl = new JLabel(" Stock Type Name        #");
		mediaTypeIdLbl.setToolTipText("Required Field");
		brandLbl = new JLabel(" Brand ");
		barcodeLbl = new JLabel(" Barcode ");
		serialNoLbl = new JLabel(" Serial No. ");
		descriptionLbl = new JLabel(" Description ");
		catIdLbl = new JLabel(" Category Description #");
		catIdLbl.setToolTipText("Required Field");
		titleLbl = new JLabel (" Title                                 #");
		titleLbl.setToolTipText("Required Field");
		statusLbl = new JLabel(" Status                            #");
		statusLbl.setToolTipText("Required Field");
		releaseDateLbl = new JLabel(" Release Date(Eg-YYYY)");
		genreLbl = new JLabel(" Genre ");
		runTimeLbl = new JLabel(" Run Time ");
		ageRestLbl = new JLabel(" Age Restriction ");
		directorLbl = new JLabel(" Director ");
		actor1Lbl = new JLabel(" Actor 1 ");
		actor2Lbl = new JLabel(" Actor 2 ");
		dateLastRentedLbl = new JLabel(" Date Last Rented ");
		
		lastJdc = new JDateChooser();
		String format = "yyyy-MM-dd";
		lastJdc.setDateFormatString(format);
		
		Vector genreV = new Vector();
		genreV.addElement("");
		genreV.addElement("Action");
		genreV.addElement("Adventure");
		genreV.addElement("Animation");
		genreV.addElement("Biography");
		genreV.addElement("Comedy");
		genreV.addElement("Crime");
		genreV.addElement("Documentary");
		genreV.addElement("Drama");
		genreV.addElement("Family");
		genreV.addElement("Fantasy");
		genreV.addElement("Film-Noir");
		genreV.addElement("Game-Show");
		genreV.addElement("History");
		genreV.addElement("Horror");
		genreV.addElement("Music");
		genreV.addElement("Musical");
		genreV.addElement("Mystery");
		genreV.addElement("News");
		genreV.addElement("Reality-TV");
		genreV.addElement("Romance");
		genreV.addElement("Sci-Fi");
		genreV.addElement("Short");
		genreV.addElement("Sport");
		genreV.addElement("Talk-Show");
		genreV.addElement("Thriller");
		genreV.addElement("War");
		genreV.addElement("Western");
		
		genreJcb = new JComboBox(genreV);
		
		Vector statusV = new Vector();
		statusV.addElement("");
		statusV.addElement("IN");	
		statusV.addElement("OUT");
		statusV.addElement("DAMAGED");
		statusV.addElement("UNAVAILABLE");
		
		statusJcb = new JComboBox(statusV);
		
		GroupLayout layout  = new GroupLayout(baseP);
		layout.setAutoCreateContainerGaps(true);
		layout.setAutoCreateGaps(true);
		baseP.setLayout(layout);
		GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
		
		//creating two vertical groups of labels and input components
		//which are alligned in parallel
		vGroup.addGroup(layout.createParallelGroup().
        addComponent(mediaStockIdLbl).addComponent(mediaTypeIdLbl).
        addComponent(titleLbl).addComponent(statusLbl).
        addComponent(barcodeLbl).addComponent(brandLbl).
        addComponent(serialNoLbl).addComponent(descriptionLbl).
        addComponent(catIdLbl).addComponent(runTimeLbl).
        addComponent(releaseDateLbl).addComponent(genreLbl).
        addComponent(ageRestLbl).addComponent(serialNoLbl).
        addComponent(directorLbl).addComponent(actor1Lbl).
        addComponent(actor2Lbl).addComponent(dateLastRentedLbl));
  	    vGroup.addGroup(layout.createParallelGroup().
        addComponent(mediaStockIdTxt).addComponent(stockTypeJcb).
        addComponent(titleTxt).addComponent(statusJcb).
        addComponent(barcodeTxt).addComponent(brandTxt).
        addComponent(serialNoTxt).addComponent(descriptionTxt).
        addComponent(catIdJcb).addComponent(runTimeTxt).
        addComponent(releaseDateTxt).addComponent(genreJcb).
        addComponent(ageRestTxt).addComponent(serialNoTxt).
        addComponent(directorTxt).addComponent(actor1Txt).
        addComponent(actor2Txt).addComponent(lastJdc));
   		layout.setHorizontalGroup(vGroup);
   		
   		GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
		hGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE,true).
        addComponent(mediaStockIdLbl).addComponent(mediaStockIdTxt));
   		hGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE,true).
        addComponent(mediaTypeIdLbl).addComponent(stockTypeJcb));
        hGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE,true).
        addComponent(titleLbl).addComponent(titleTxt));
        hGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE,true).
        addComponent(statusLbl).addComponent(statusJcb));
        hGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE,true).
        addComponent(barcodeLbl).addComponent(barcodeTxt));
        hGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE,true).
        addComponent(brandLbl).addComponent(brandTxt));
        hGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE,true).
        addComponent(serialNoLbl).addComponent(serialNoTxt));
        hGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE,true).
        addComponent(descriptionLbl).addComponent(descriptionTxt));
        hGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE,true).
        addComponent(catIdLbl).addComponent(catIdJcb));
        hGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE,true).
        addComponent(runTimeLbl).addComponent(runTimeTxt));
        hGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE,true).
        addComponent(releaseDateLbl).addComponent(releaseDateTxt));
        hGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE,true).
        addComponent(genreLbl).addComponent(genreJcb));
        hGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE,true).
        addComponent(ageRestLbl).addComponent(ageRestTxt));
        hGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE,true).
        addComponent(directorLbl).addComponent(directorTxt));
        hGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE,true).
        addComponent(actor1Lbl).addComponent(actor1Txt));
        hGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE,true).
        addComponent(actor2Lbl).addComponent(actor2Txt));
        hGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE,true).
        addComponent(dateLastRentedLbl).addComponent(lastJdc));
        layout.setVerticalGroup(hGroup);
        
        baseP.setBorder(BorderFactory.createCompoundBorder(
			BorderFactory.createEmptyBorder(1,1,0,1),
			BorderFactory.createRaisedBevelBorder()));
        this.add(baseP);
	}
	
	//gets the stock types that are in the DB and populates the vector
	public void populateStockTypeV()
	{
		try
		{
			Statement stmt = Conn.createStatement();
			ResultSet rs = stmt.executeQuery(queryStockType);
			rs.last();
			int num = rs.getRow();
			rs.first();
			stockTypeV.addElement("");	
			for(int i =1; i <=num; i++)
			{
				stockTypeV.addElement(rs.getString(1));
				rs.next();
			} 
		}
		catch(SQLException sqle)
		{
			sqle.printStackTrace();
		}
		
	}
	
	//gets the category Names that are in the DB and populates the vector
	public void populateCatV()
	{
		try
		{
			Statement stmt = Conn.createStatement();
			ResultSet rs = stmt.executeQuery(queryCatId);
			rs.last();
			int num = rs.getRow();
			rs.first();
			catIdV.addElement("");
			for(int i =1; i <=num; i++)
			{
				catIdV.addElement(rs.getString(1));
				rs.next();
			}
		}
		catch(SQLException sqle)
		{
			sqle.printStackTrace();
		}
		 
	}
	public java.util.Date getJdcDate()
	{
		return lastJdc.getDate();
	}
	public String getMediaTypeId()
	{
		return mediaTypeIdTxt.getText();
	}
}
